package com.sneakxy.config;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import com.sneakxy.jdbc.Dialect;
import com.sneakxy.jdbc.JdbcHelper;

@XmlAccessorType(XmlAccessType.FIELD)
public class Jdbc {

	@XmlElement(name="dialect")
	private String dialect;

	@XmlElement(name="driver")
	private String driver;
	
	@XmlElement(name="url")
	private String url;
	
	@XmlElement(name="username")
	private String username;
	
	@XmlElement(name="password")
	private String password;

	public JdbcHelper createJdbcHelper() {
		Dialect dialect = createDialect();
		dialect.setUrl(getUrl());
		return new JdbcHelper(this, dialect);
	}
	
	protected Dialect createDialect() {
		Object result = null;
		try {
			result = Class.forName(this.getDialect()).newInstance();
			System.out.println("dialect create success!");
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			System.out.println("create dialect error!");
			e.printStackTrace();
		}
		return (Dialect) result;
	}
	
	public String getDialect() {
		return dialect;
	}

	public void setDialect(String dialect) {
		this.dialect = dialect;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
